﻿/*
 * FileName: Tournament.cs
 * Module: Core
 * Author: Gourav Das
 * Created: 04/2011
 * Modifications:
 * Copyright: Gourav Das, 2011
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace _22yards.Core
{
    /// <summary>
    /// Class used by Class Groups. This holds information like TeamID, Points and NRR
    /// </summary>
    public class TeamStandings
    {
        #region Properties

        /// <summary>
        /// Gets or sets the TeamID
        /// </summary>
        /// <value>
        /// 8 Digit ID of Team
        /// </value>
        public int TeamID { get; set; }

        /// <summary>
        /// Gets or sets the Points
        /// </summary>
        /// <value>
        /// Points Earned during this Tournament
        /// </value>
        public int Points { get; set; }

        /// <summary>
        /// Gets or Sets the NRR
        /// </summary>
        /// <value>
        /// Net Run Rate after each Match.
        /// </value>
        public decimal NRR { get; set; }

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="TeamStandings"/> class.
        /// </summary>
        public TeamStandings()
        {
            TeamID = 0;
            Points = 0;
            NRR = 0;
        }

        /// <summary>
        /// Resets Points and NRR to 0
        /// </summary>
        public void Reset()
        {
            Points = 0;
            NRR = 0;
        }

        /// <summary>
        /// Clears all values to 0
        /// </summary>
        public void clear()
        {
            TeamID = 0;
            Reset();
        }
    }

    /// <summary>
    /// Class used by Tournament. Consists information about teams in groups and their standings
    /// </summary>
    public class Group
    {
        #region Properties
        /// <summary>
        /// Gets or sets the name of the group.
        /// </summary>
        /// <value>
        /// The name of the group.
        /// </value>
        public string GroupName { get; set; }

        /// <summary>
        /// Gets or sets the teams standings.
        /// </summary>
        /// <value>
        /// Instance of TeamStandings class
        /// </value>
        public List<TeamStandings> TeamsStandings { get; set; }

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="Group"/> class.
        /// </summary>
        public Group()
        {
            GroupName = string.Empty;
            TeamsStandings = new List<TeamStandings>();
        }

        /// <summary>
        /// Clears this instance.
        /// </summary>
        public void Clear()
        {
            GroupName = string.Empty;
            TeamsStandings.Clear();
        }

    }

    /// <summary>
    /// Class to hold information about tournaments
    /// </summary>
    public class Tournament
    {
        #region Members

        /// <summary>
        /// A list of Matches in order of Itinerary
        /// </summary>
        public List<Match> MatchItinerary;

        /// <summary>
        /// A list of Time when game in the itinerary is going to be played, Possible values - 0, 1, 2, 3
        /// </summary>
        public List<short> TimeOfPlay;

        /// <summary>
        /// List of Teams in this Tournament
        /// </summary>
        public List<Team> Teams;

        /// <summary>
        /// List of Groups in this Tournament
        /// </summary>
        public List<Group> Groups;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the name of the file.
        /// </summary>
        /// <value>
        /// The name of the file from which Tournament would be loaded.
        /// </value>
        public string File {get; set;}


        /// <summary>
        /// Gets or sets a value indicating whether [use UniformMatchType].
        /// </summary>
        /// <value>
        /// 	<c>true</c> if [UniformMatchType] is selected in gameplay screen; otherwise, <c>false</c>.
        /// </value>
        public bool UniformMatchType { get; set; }

        /// <summary>
        /// Gets or sets the no of qualifying teams in group.
        /// </summary>
        /// <value>
        /// The no of qualifying teams in each group. This is got from the XML attribute GroupLeadersApplicable
        /// </value>
        public short NoOfQualifyingTeamsInGroup { get; set; }

        #endregion

        /// <summary>
        /// Initializes the tournament.
        /// </summary>
        public void initTournament()
        {
            MatchItinerary = new List<Match>();
            Groups = new List<Group>();
            UniformMatchType = true;
            
        }

        /// <summary>
        /// Loads the teams into groups from the Itinerary file.
        /// </summary>
        /// <param name="filename">The filename of the Itinerary with extension.</param>
        public void LoadTeamsIntoGroups()
        {
            Group group = new Group();

            XmlTextReader xmlReader = new XmlTextReader(@"..\..\Itinerary\" + File);
            while (xmlReader.Read())
            {
                switch (xmlReader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (xmlReader.Name == "GROUP")
                        {                      
                            group.Clear();
                            group.GroupName = "Group " + xmlReader.GetAttribute("ID");
                        }

                        if (xmlReader.Name == "TEAM")
                        {
                            TeamStandings tmstnd=new TeamStandings();
                            tmstnd.TeamID = (Convert.ToInt32(xmlReader.GetAttribute("ID")));
                            group.TeamsStandings.Add(tmstnd);
                        }
                        break;

                    case XmlNodeType.EndElement:
                        if (xmlReader.Name == "GROUP")
                        {
                            Groups.Add(group);
                        }
                        break;

                }
            }
        }

        /// <summary>
        /// Loads the matches from Tournament file.
        /// </summary>
        public void LoadMatchesFromFile()
        {
            short matchType=0;
            bool TeamsFinalized = false;
            Match match;

            XmlTextReader xmlReader = new XmlTextReader(@"..\..\Itinerary\" + File);
            while (xmlReader.Read())
            {
                switch (xmlReader.NodeType)
                {
                    case XmlNodeType.Element:

                        if(xmlReader.Name == "TOURNAMENT")
                        {
                            matchType=Convert.ToInt16(xmlReader.GetAttribute("type"));
                            NoOfQualifyingTeamsInGroup = Convert.ToInt16(xmlReader.GetAttribute("GroupLeadersApplicable"));
                        }

                        if (xmlReader.Name == "MATCHES")
                        {
                            TeamsFinalized = Convert.ToBoolean(xmlReader.GetAttribute("teamsfinalized"));
                        }

                        if (xmlReader.Name == "MATCH")
                        {
                            int Team1ID;
                            int Team2ID;

                            Team1ID=Convert.ToInt32(xmlReader.GetAttribute("team1"));
                            Team2ID=Convert.ToInt32(xmlReader.GetAttribute("team2"));

                            Team T1 = new Team();
                            Team T2 = new Team();

                            foreach(Team t1 in Teams)
                            {
                                if (t1.TeamNumber == Team1ID)
                                    T1=t1;
                                else if(t1.TeamNumber == Team2ID)
                                    T2=t1;
                            }

                            if (TeamsFinalized)
                            {
                                if (UniformMatchType)
                                    match = new Match(T1, T2, matchType);
                                else
                                    match = new Match(T1, T2, 0);
                            }
                            else
                            {
                                if (UniformMatchType)
                                    match = new Match(matchType);
                                else
                                    match = new Match(0);
                            }

                            match.matchState = Convert.ToInt16(xmlReader.Value);
                            match.MatchInGroup = xmlReader.GetAttribute("group");
                            match.AbsoluteMatchDay = Convert.ToInt16(xmlReader.GetAttribute("day"));
                            match.MatchTime = Convert.ToInt16(xmlReader.GetAttribute("time"));

                            MatchItinerary.Add(match);
                        }
                        break;
                }
            }

        }

        void loadSavedTournament(string path)
        {
            //Load All teams into List<Team> Teams
            //Load all Matches into List<Match> MatchItinerary
            //Set Match State appropriately
            throw new NotImplementedException();
        }

        void saveTournament(string path)
        {
            throw new NotImplementedException();
        }

    }
}
